import javax.jcr.Session
import javax.jcr.Repository
import javax.jcr.Node as JCRNode
import org.apache.jackrabbit.core.TransientRepository
import javax.jcr.SimpleCredentials
import ch.bluepenguin.jcr.groovy.instrumentation.JcrInstrumentator

class TestA {
    String a
    def b
}

/**
* Created by IntelliJ IDEA.
* User: christian
* Date: Mar 17, 2008
* Time: 8:39:22 PM
* To change this template use File | Settings | File Templates.
*/
class JcrInstrumentatorTests extends GroovyTestCase  {
    static Session session=null


    static {
      if(!session) {
      Repository repository = new TransientRepository()
      //Session session = repository.login()
      session = repository.login(
                new SimpleCredentials("username", "password".toCharArray()))
      }
        }

    void testGroovy() {
        JcrInstrumentator instrumentator = new JcrInstrumentator()
        instrumentator.session = session
        instrumentator.instrumentate(TestA)
        TestA a = new TestA(a:"abc", b: new String())
        a.jcrPath ="testA"
        try {
            a.persist()
        }finally{
            session.rootNode.getNode(a.jcrPath).remove()
            session.save()
        }
    }

        void testJava() {
            JcrInstrumentator instrumentator = new JcrInstrumentator()
            instrumentator.strategy = JcrInstrumentator.STRATEGY_JAVA
            instrumentator.session = session
           // JCRNode node = session.rootNode
        instrumentator.instrumentate(session)
            session.jcrPath ="blabla"
        try {
            session.persist()
            assert session.userID == session.rootNode.getNode(session.jcrPath).getProperty("userID").getString()
        }finally{
            session.rootNode.getNode(session.jcrPath).remove()
            session.save()
        }


    }
} 